<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是shell——sed命令的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV02-02-shell-07-sed命令的使用">
<meta property="og:url" content="https://sumumm.github.io/post/9720d703.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是shell——sed命令的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-02-22T00:22:02.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.988Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV02-编译基础">
<meta property="article:tag" content="shell脚本">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://sumumm.github.io/post/9720d703.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/9720d703.html","path":"post/9720d703.html","title":"LV02-02-shell-07-sed命令的使用"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV02-02-shell-07-sed命令的使用 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81sed-%E7%AE%80%E4%BB%8B"><span class="nav-text">一、sed 简介</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81-sed-%E5%91%BD%E4%BB%A4"><span class="nav-text">二、 sed 命令</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81sed-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">三、sed 使用实例</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-a-%E5%92%8C-i-%E5%8A%A8%E4%BD%9C"><span class="nav-text">1. a 和 i 动作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E5%9C%A8%E7%AC%AC-n-%E8%A1%8C%E5%89%8D%E6%8F%92%E5%85%A5%E4%B8%80%E8%A1%8C"><span class="nav-text">1.1 在第 n 行前插入一行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E5%9C%A8%E7%AC%AC-n-%E8%A1%8C%E5%90%8E%E6%8F%92%E5%85%A5%E4%B8%80%E8%A1%8C"><span class="nav-text">1.2 在第 n 行后插入一行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E5%9C%A8%E6%9C%80%E5%90%8E%E4%B8%80%E8%A1%8C%E5%89%8D%E6%8F%92%E5%85%A5%E4%B8%80%E8%A1%8C"><span class="nav-text">1.3 在最后一行前插入一行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-%E5%9C%A8%E6%9C%80%E5%90%8E%E4%B8%80%E8%A1%8C%E5%90%8E%E6%8F%92%E5%85%A5%E4%B8%80%E8%A1%8C"><span class="nav-text">1.4 在最后一行后插入一行</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-d-%E5%8A%A8%E4%BD%9C"><span class="nav-text">2. d 动作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E9%80%9A%E8%BF%87%E8%A1%8C%E5%8F%B7%E6%8C%87%E5%AE%9A%E5%88%A0%E9%99%A4%E8%A1%8C"><span class="nav-text">2.1 通过行号指定删除行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E9%80%9A%E8%BF%87%E5%8C%BA%E9%97%B4%E5%88%A0%E9%99%A4%E8%A1%8C"><span class="nav-text">2.2 通过区间删除行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E9%80%9A%E8%BF%87%E6%95%B0%E6%8D%AE%E6%90%9C%E7%B4%A2%E5%88%A0%E9%99%A4%E8%A1%8C"><span class="nav-text">2.3 通过数据搜索删除行</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-c-%E5%8A%A8%E4%BD%9C"><span class="nav-text">3. c 动作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E9%80%9A%E8%BF%87%E8%A1%8C%E5%8F%B7%E6%8C%87%E5%AE%9A%E6%9B%BF%E6%8D%A2%E8%A1%8C"><span class="nav-text">3.1 通过行号指定替换行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E9%80%9A%E8%BF%87%E5%8C%BA%E9%97%B4%E6%8C%87%E5%AE%9A%E6%9B%BF%E6%8D%A2%E8%A1%8C"><span class="nav-text">3.2 通过区间指定替换行</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-n%E9%80%89%E9%A1%B9%E4%B8%8Ep%E5%8A%A8%E4%BD%9C"><span class="nav-text">4. -n选项与p动作 </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-%E6%95%B0%E6%8D%AE%E7%9A%84%E6%90%9C%E7%B4%A2%E6%98%BE%E7%A4%BA"><span class="nav-text">4.1 数据的搜索显示</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E5%8A%A0%E4%B8%8A-n"><span class="nav-text">4.2 加上 -n ?</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-s-%E5%8A%A8%E4%BD%9C"><span class="nav-text">5.  s 动作 </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">5.1 使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-%E5%AE%9E%E4%BE%8B1%EF%BC%9A%E6%9B%BF%E6%8D%A2%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84-n-%E4%B8%8E-g-%E6%A0%87%E8%AF%86"><span class="nav-text">5.2 实例1：替换字符串的 n 与 g 标识</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-3-%E5%AE%9E%E4%BE%8B2%EF%BC%9A%E6%98%BE%E7%A4%BA%E6%9B%BF%E6%8D%A2%E8%A1%8C%E7%9A%84-p-%E6%A0%87%E8%AF%86"><span class="nav-text">5.3 实例2：显示替换行的 p 标识</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-4-%E5%AE%9E%E4%BE%8B3%EF%BC%9A%E5%8C%B9%E9%85%8D%E7%BB%93%E6%9E%9C%E8%BF%9B%E8%A1%8C%E4%BF%9D%E5%AD%98%E7%9A%84-w-file-%E6%A0%87%E8%AF%86"><span class="nav-text">5.4 实例3：匹配结果进行保存的 w file 标识</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-5-%E5%AE%9E%E4%BE%8B4%EF%BC%9A%E8%BD%AC%E4%B9%89%E6%A0%87%E8%AF%86"><span class="nav-text">5.5 实例4：转义标识 \</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-5-%E5%AE%9E%E4%BE%8B5%EF%BC%9A%E6%95%B0%E6%8D%AE%E6%90%9C%E7%B4%A2%E5%B9%B6%E6%98%BE%E7%A4%BA%E5%B9%B6%E6%89%A7%E8%A1%8C%E5%91%BD%E4%BB%A4"><span class="nav-text">5.5 实例5：数据搜索并显示并执行命令</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-y%E5%8A%A8%E4%BD%9C"><span class="nav-text">6. y动作 </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1-%E4%BD%BF%E7%94%A8%E6%A0%BC%E5%BC%8F"><span class="nav-text">6.1 使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">6.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-r-%E5%8A%A8%E4%BD%9C"><span class="nav-text">7. r 动作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-e-%E9%80%89%E9%A1%B9"><span class="nav-text">8. -e 选项 </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-i-%E9%80%89%E9%A1%B9-%E6%85%8E%E7%94%A8"><span class="nav-text">9. -i 选项(慎用) </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-f-%E9%80%89%E9%A1%B9"><span class="nav-text">10. -f 选项 </span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E4%BD%BF%E7%94%A8%E5%8F%98%E9%87%8F%EF%BC%9F"><span class="nav-text">四、使用变量？</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%91%BD%E4%BB%A4%E7%94%A8%E5%8F%8C%E5%BC%95%E5%8F%B7"><span class="nav-text">1. 命令用双引号</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%91%BD%E4%BB%A4%E7%94%A8%E5%8D%95%E5%BC%95%E5%8F%B7"><span class="nav-text">2. 命令用单引号</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81%E7%A9%BA%E6%96%87%E4%BB%B6%E4%B8%8D%E7%94%9F%E6%95%88%EF%BC%9F"><span class="nav-text">五、空文件不生效？</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/9720d703.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV02-02-shell-07-sed命令的使用 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV02-02-shell-07-sed命令的使用
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-02-22 08:22:02" itemprop="dateCreated datePublished" datetime="2022-02-22T08:22:02+08:00">2022-02-22</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">LV02-编译基础</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>8.2k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>30 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是shell——sed命令的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/LV02-02-shell-07-sed%E5%91%BD%E4%BB%A4%E7%9A%84%E4%BD%BF%E7%94%A8/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">---</td><td align="left"><a href="" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、sed-简介"><a href="#一、sed-简介" class="headerlink" title="一、sed 简介"></a><font size=3>一、sed 简介</font></h1><p>sed 算是是一种在线编辑器，采用的是流编辑模式，它一次处理文件中一行的内容。进行文本处理时，会把当前处理的行存储在<strong>临时缓冲区</strong>中，称为“模式空间”（ pattern space ），接着用 sed 命令处理缓冲区中的内容，处理完成后，把缓冲区的内容<strong>送往屏幕</strong>，所以每处理一次就会有一次输出。接着处理下一行，这样不断重复，直到文件末尾。</p>
<p><strong>【注意】</strong> sed  默认不会直接修改源文件数据，而是会将数据复制到缓冲区中，修改也仅限于缓冲区中的数据。</p>
<h1 id="二、-sed-命令"><a href="#二、-sed-命令" class="headerlink" title="二、 sed 命令"></a><font size=3>二、 sed 命令</font></h1><p>sed 命令，不管是否找到指定的模式，它的退出状态都是 0 。只有当命令存在语法错误时， sed 的退出状态才不是 0 。语法格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sed [ option ] [ action ] file_name</span><br></pre></td></tr></table></figure>

<ul>
<li>option</li>
</ul>
<table>
    <tr>
        <td align="center" rowspan="4">option</td>
        <td align="center" width=150px>-n</td>
        <td align="left">该选项会屏蔽启动输出，仅显示script处理后的结果</td>
    </tr>
    <tr>
        <td align="center" width=150px>-i</td>
        <td align="left">直接修改文件内容，而不是输出到终端。</td>
    </tr>
    <tr>
        <td align="center" width=150px>-e [script] </td>
        <td align="left">以选项中指定的script来处理输入的文本文件，若之前已有命令，此时会将script脚本命令添加到已有的命令中。</td>
    </tr>
    <tr>
        <td align="center" width=150px>-f [script文件] </td>
        <td align="left">以选项中指定的script文件来处理输入的文本文件，若之前已有命令，此时会将script文件中的脚本命令添加到已有的命令中。</td>
    </tr>
</table>

<p><strong>【注意】</strong> -i 选项要慎用，毕竟会直接修改原文件。</p>
<ul>
<li>action</li>
</ul>
<table>
    <tr>
        <td align="center" rowspan="8">action</td>
        <td align="center" width=50px>a</td>
        <td align="left">新增， a 的后面可以接字符串，这些字串会在新的一行出现(当前行的下一行)。<br>【注意】若要插入多行，则行之间要加上 \ 。</td>
    </tr>
    <tr>
        <td align="center" width=50px>c</td>
        <td align="left">替换整行，用此符号后的新文本替换当前行中的文本。</td>
    </tr>
    <tr>
        <td align="center" width=50px>d</td>
        <td align="left">删除，通常不接任何东西</td>
    </tr>
    <tr>
        <td align="center" width=50px>i</td>
        <td align="left">插入，后面可以跟字符串，在当前行的上一行插入文本<br>【注意】若要插入多行，则行之间要加上 \ 。</td>
    </tr>
    <tr>
        <td align="center" width=50px>p</td>
        <td align="left">打印，通常与 -n 一起使用。</td>
    </tr>
    <tr>
        <td align="center" width=50px>s</td>
        <td align="left">替换字符串，可以搭配正则表达式，用一个字符串替换另一个字符串</td>
    </tr>
    <tr>
        <td align="center" width=50px>y</td>
        <td align="left">将字符替换为另一字符（不能对正则表达式使用y命令）</td>
    </tr>
    <tr>
        <td align="center" width=50px>r</td>
        <td align="left">将一个独立文件的数据插入到当前数据流的指定位置</td>
    </tr>
</table>

<p>【注意】 sed  后面接的动作，要以  ‘ ‘  两个单引号括住。</p>
<ul>
<li>相关正则表达式</li>
</ul>
<table>
    <tr>
        <td align="center" width=100px>字符</td>
        <td align="center" width=200px>说明</td>
        <td align="center">实例</td>
    </tr>
    <tr>
        <td align="center">^</td>
        <td align="left">行首定位符</td>
        <td align="left"> /^a/  匹配所有以a开头的行。</td>
    </tr>
    <tr>
        <td align="center">$</td>
        <td align="left">行尾定位符</td>
        <td align="left"> /a$/  匹配所有以 a 结尾的行。</td>
    </tr>
    <tr>
        <td align="center">.</td>
        <td align="left">匹配除换行符以外的单个字符</td>
        <td align="left"> /a..b/  匹配包含字母 a ，后跟两个任意字符，再跟字母 b 的行。</td>
    </tr>
    <tr>
        <td align="center">*</td>
        <td align="left">匹配除换行符以外的单个字符</td>
        <td align="left"> /a..b/  匹配包含字母 a ，后跟两个任意字符，再跟字母 b 的行</td>
    </tr>
    <tr>
        <td align="center">[]</td>
        <td align="left">匹配指定字符组内的任一字符</td>
        <td align="left"> /[Aa]b/  匹配包含 Ab 或 ab 的行。</td>
    </tr>
    <tr>
        <td align="center">[^]</td>
        <td align="left">匹配不在指定字符组内的任一字符</td>
        <td align="left">  /[^Aa]b/  匹配包含 b ，但 b 之前的那个字符不是 A 或 a 的行。</td>
    </tr>
    <tr>
        <td align="center">\(..\)</td>
        <td align="left">保存已匹配的字符</td>
        <td align="left"> 1,5s/\(you\)self/\1r/ 标记元字符之间的模式，并将其保存为标签 1 ，之后可以使用 \1 来引用它。最多可以定义 9 个标签，从左边开始编号，最左边的是第一个。此例中，对第 1 到第 5 行进行处理，you被保存为标签 1 ，如果发现 youself ，则替换为 your 。</td>
    </tr>
    <tr>
        <td align="center">&</td>
        <td align="left"> 保存查找的字符串以便在替换串中引用</td>
        <td align="left"> s/ab/**&**/  符号 & 代表查找的字符串，ab 将会被替换为**ab**。</td>
    </tr>
    <tr>
        <td align="center"> \< </td>
        <td align="left">词首定位符</td>
        <td align="left">/\&lt;ab/  匹配包含以 ab 开头的单词的行。</td>
    </tr>
    <tr>
        <td align="center"> \> </td>
        <td align="left">词尾定位符</td>
        <td align="left"> /ab\>/  匹配包含以 ab 结尾的单词的行。</td>
    </tr>
    <tr>
        <td align="center"> a\{n\} </td>
        <td align="left">连续 n 个 a </td>
        <td align="left"> /a\{5\}/ 匹配包含连续 5 个 a 的行。</td>
    </tr>
    <tr>
        <td align="center"> a\{n,\} </td>
        <td align="left">至少连续 n 个 a </td>
        <td align="left"> /a\{5,\}/  匹配包含至少连续 5 个 a 的行。</td>
    </tr>
    <tr>
        <td align="center"> a\{m,n\} </td>
        <td align="left">至少连续 m 个，但不超过连续 n 个 a </td>
        <td align="left">  /a\{5,7\}/  匹配包含连续 5 到 7 个 a 的行。</td>
    </tr>
</table>


<h1 id="三、sed-使用实例"><a href="#三、sed-使用实例" class="headerlink" title="三、sed 使用实例"></a><font size=3>三、sed 使用实例</font></h1><p>只说参数并不能很好理解如何使用，这里将会有大量的实例来帮忙理解。先写一个测试用的文件，就以 Hexo 的站点配置文件为例，删除一些东西，留下一些用于测试使用。文件内容如下：</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">[<span class="number">1</span>] <span class="attr">title:</span> <span class="string">Hexo</span></span><br><span class="line">[<span class="number">2</span>] <span class="attr">author:</span> <span class="string">John</span> <span class="string">Doe</span></span><br><span class="line">[<span class="number">3</span>] <span class="attr">language:</span> <span class="string">en</span></span><br><span class="line">[<span class="number">4</span>] <span class="attr">url:</span> <span class="string">http://example.com</span></span><br><span class="line">[<span class="number">5</span>] <span class="attr">permalink:</span> <span class="string">:year/:month/:day/:title/</span></span><br><span class="line">[<span class="number">6</span>]</span><br><span class="line">[<span class="number">7</span>] <span class="attr">theme: theme:</span> <span class="string">landscape</span> <span class="string">theme</span></span><br><span class="line">[<span class="number">8</span>] <span class="attr">theme:</span> <span class="string">aaa</span></span><br><span class="line">[<span class="number">9</span>] <span class="attr">theme:</span> <span class="string">landscape</span></span><br><span class="line">[<span class="number">10</span>]<span class="attr">theme:</span> <span class="string">landscape</span></span><br><span class="line">[<span class="number">11</span>]<span class="attr">Theme:</span> <span class="string">def</span></span><br></pre></td></tr></table></figure>

<p>【说明】我这里用的是 Windows 下 Git 的 bash ，与 linux 中应该是一样的。另外运行脚本中的 $ .&#x2F;1.sh 表示在命令行运行该脚本文件，下边的为输出结果，文件中前边方括号里边的内容为这一行在源文件的行号。</p>
<h2 id="1-a-和-i-动作"><a href="#1-a-和-i-动作" class="headerlink" title="1. a 和 i 动作"></a><font size=3>1. a 和 i 动作</font></h2><h3 id="1-1-在第-n-行前插入一行"><a href="#1-1-在第-n-行前插入一行" class="headerlink" title="1.1 在第 n 行前插入一行"></a><font size=3>1.1 在第 n 行前插入一行</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;ni\string&#x27;</span> file_name</span></span><br><span class="line">sed &#x27;2i\这是第2行前新插入的行&#x27; ./_config.yml</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            这是第2行前新插入的行 <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
    </tr>
</table>

<h3 id="1-2-在第-n-行后插入一行"><a href="#1-2-在第-n-行后插入一行" class="headerlink" title="1.2 在第 n 行后插入一行"></a><font size=3>1.2 在第 n 行后插入一行</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;na\string&#x27;</span> file_name</span></span><br><span class="line">sed &#x27;2a\这是第2行后新插入的行&#x27; ./_config.yml</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            这是第2行后新插入的行 <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
    </tr>
</table>

<h3 id="1-3-在最后一行前插入一行"><a href="#1-3-在最后一行前插入一行" class="headerlink" title="1.3 在最后一行前插入一行"></a><font size=3>1.3 在最后一行前插入一行</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;$i\string&#x27;</span> file_name</span></span><br><span class="line">sed &#x27;$i\这是最后一行行前新插入的行&#x27; ./_config.yml</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            这是最后一行行前新插入的行<br>
            [11]Theme: def <br>
        </td>
    </tr>
</table>

<h3 id="1-4-在最后一行后插入一行"><a href="#1-4-在最后一行后插入一行" class="headerlink" title="1.4 在最后一行后插入一行"></a><font size=3>1.4 在最后一行后插入一行</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;$a\string&#x27;</span> file_name</span></span><br><span class="line">sed &#x27;$a\这是最后一行行前新插入的行&#x27; ./_config.yml</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
            这是最后一行行后新插入的行<br>
        </td>
    </tr>
</table>

<h2 id="2-d-动作"><a href="#2-d-动作" class="headerlink" title="2. d 动作"></a><font size=3>2. d 动作</font></h2><h3 id="2-1-通过行号指定删除行"><a href="#2-1-通过行号指定删除行" class="headerlink" title="2.1 通过行号指定删除行"></a><font size=3>2.1 通过行号指定删除行</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;nd&#x27;</span> filename</span></span><br><span class="line">sed &#x27;2d&#x27; ./_config.yml # 删除第2行</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
    </tr>
</table>

<h3 id="2-2-通过区间删除行"><a href="#2-2-通过区间删除行" class="headerlink" title="2.2 通过区间删除行"></a><font size=3>2.2 通过区间删除行</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;n1,n2d&#x27;</span> filename</span></span><br><span class="line">sed &#x27;2,4d&#x27; ./_config.yml # 删除2-4行，共删除3行</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
    </tr>
</table>

<h3 id="2-3-通过数据搜索删除行"><a href="#2-3-通过数据搜索删除行" class="headerlink" title="2.3 通过数据搜索删除行"></a><font size=3>2.3 通过数据搜索删除行</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;/&lt;string&gt;/d&#x27;</span> filename</span></span><br><span class="line">sed &#x27;/theme/d&#x27; ./_config.yml # 删除含有theme的行</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [11]Theme: def <br>
        </td>
    </tr>
</table>


<h2 id="3-c-动作"><a href="#3-c-动作" class="headerlink" title="3. c 动作"></a><font size=3>3. c 动作</font></h2><h3 id="3-1-通过行号指定替换行"><a href="#3-1-通过行号指定替换行" class="headerlink" title="3.1 通过行号指定替换行"></a><font size=3>3.1 通过行号指定替换行</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;2c &lt;string&gt;&#x27;</span> filename</span></span><br><span class="line">sed &#x27;2c 这是替换后的第2行&#x27; ./_config.yml</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            这是替换后的第2行<br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
    </tr>
</table>

<h3 id="3-2-通过区间指定替换行"><a href="#3-2-通过区间指定替换行" class="headerlink" title="3.2 通过区间指定替换行"></a><font size=3>3.2 通过区间指定替换行</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;n1,n2c &lt;string&gt;&#x27;</span> filename</span></span><br><span class="line">sed &#x27;2,3c 2,3行已被替换，这是替换后的行&#x27; ./_config.yml</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            2,3行已被替换，这是替换后的行<br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
    </tr>
</table>


<h2 id="4-n选项与p动作"><a href="#4-n选项与p动作" class="headerlink" title="4. -n选项与p动作 "></a><font size=3>4. -n选项与p动作 </font></h2><h3 id="4-1-数据的搜索显示"><a href="#4-1-数据的搜索显示" class="headerlink" title="4.1 数据的搜索显示"></a><font size=3>4.1 数据的搜索显示</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;/&lt;string&gt;/p&#x27;</span> filename</span></span><br><span class="line">sed &#x27;/theme/p&#x27; ./_config.yml # 显示含有 theme 的行</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
    </tr>
</table>




<h3 id="4-2-加上-n"><a href="#4-2-加上-n" class="headerlink" title="4.2 加上 -n ?"></a><font size=3>4.2 加上 -n ?</font></h3><p>我们会发现上边的输出中，含有 theme 的行每行都被显示了两次，一次是处理的时候的输出，另一次是 p 动作的输出。那么如何只输出我所需要的行呢？</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed -n <span class="string">&#x27;/&lt;string&gt;/p&#x27;</span> filename</span></span><br><span class="line">sed -n &#x27;/theme/p&#x27; ./_config.yml # 显示含有 theme的行</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
        </td>
    </tr>
</table>

<h2 id="5-s-动作"><a href="#5-s-动作" class="headerlink" title="5.  s 动作 "></a><font size=3>5.  s 动作 </font></h2><h3 id="5-1-使用格式"><a href="#5-1-使用格式" class="headerlink" title="5.1 使用格式"></a><font size=3>5.1 使用格式</font></h3><p> s 就是字符串替换的命令，使用格式如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">s/original/target/flags</span><br></pre></td></tr></table></figure>

<table>
    <tr>
        <td align="center" width=100px>original</td>
        <td align="left" colspan="2">需要替换的字符串，也就是原来的字符串</td>
    </tr>
    <tr>
        <td align="center">target</td>
        <td align="left" colspan="2">替换后的字符串，也就是目标字符串</td>
    </tr>
    <tr>
        <td align="center" rowspan="7">flags</td>
        <td align="center" width=80px>n</td>
        <td align="left">1 ~ 512 之间的数字，表示指定要替换的字符串出现第几次时才进行替换，例如，一行中有 5 个 a，但用户只想替换第 3 个 a，就可以使用此标记。</td>
    </tr>
    <tr>
        <td align="center" width=80px>g</td>
        <td align="left">对数据中所有匹配到的内容进行替换，如果没有 g，则只会在第 1 次匹配成功时做替换操作。</td>
    </tr>
    <tr>
        <td align="center">p</td>
        <td align="left">会打印与替换命令中指定的模式匹配的行。此标记通常与 -n 选项一起使用。</td>
    </tr>
    <tr>
        <td align="center" width=80px>w file</td>
        <td align="left">将缓冲区中的内容写到指定的 file 文件中。</td>
    </tr>
    <tr>
        <td align="center" width=80px>&</td>
        <td align="left">用正则表达式匹配的内容进行替换。</td>
    </tr>
    <tr>
        <td align="center" width=80px>\n</td>
        <td align="left">匹配第 n 个子串，该子串之前在 original 中用 \(\) 指定。</td>
    </tr>
    <tr>
        <td align="center" width=80px>\</td>
        <td align="left">转义（转义替换部分包含：&、\ 等）。</td>
    </tr>
</table>

<h3 id="5-2-实例1：替换字符串的-n-与-g-标识"><a href="#5-2-实例1：替换字符串的-n-与-g-标识" class="headerlink" title="5.2 实例1：替换字符串的 n 与 g 标识"></a><font size=3>5.2 实例1：替换字符串的 n 与 g 标识</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;s/original/target/g&#x27;</span> filename</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">     sed <span class="string">&#x27;s/original/target/n&#x27;</span> filename</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed <span class="string">&#x27;s/theme/我是替换的字符串/g&#x27;</span> ./_config.yml <span class="comment"># 替换匹配到的所有 theme</span></span></span><br><span class="line">sed &#x27;s/theme/我是替换的字符串/2&#x27; ./_config.yml # 替换匹配到的第2个 theme</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: 我是替换的字符串: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
    </tr>
</table>


<p>【说明】 -g 选项则是替换所有的匹配值，这里不再说明。</p>
<h3 id="5-3-实例2：显示替换行的-p-标识"><a href="#5-3-实例2：显示替换行的-p-标识" class="headerlink" title="5.3 实例2：显示替换行的 p 标识"></a><font size=3>5.3 实例2：显示替换行的 p 标识</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed -n <span class="string">&#x27;s/original/target/p&#x27;</span> filename</span></span><br><span class="line">sed -n &#x27;s/theme/我是替换的字符串/p&#x27; ./_config.yml</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [7] 我是替换的字符串: theme: landscape theme <br>
            [8] 我是替换的字符串: aaa <br>
            [9] 我是替换的字符串: landscape <br>
            [10]我是替换的字符串: landscape <br>
        </td>
    </tr>
</table>


<p>【说明】此时仅仅输出了替换的行，但是，只要含有 theme 的行，每行的第一个 theme 都会被替换。</p>
<h3 id="5-4-实例3：匹配结果进行保存的-w-file-标识"><a href="#5-4-实例3：匹配结果进行保存的-w-file-标识" class="headerlink" title="5.4 实例3：匹配结果进行保存的 w file 标识"></a><font size=3>5.4 实例3：匹配结果进行保存的 w file 标识</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;s/original/target/w test.txt&#x27;</span> filename</span></span><br><span class="line">sed -n &#x27;s/theme/我是替换的字符串/w test.txt&#x27; ./_config.yml</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">text.txt文件内容(屏幕不显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [7] 我是替换的字符串: theme: landscape theme <br>
            [8] 我是替换的字符串: aaa <br>
            [9] 我是替换的字符串: landscape <br>
            [10]我是替换的字符串: landscape <br>
        </td>
    </tr>
</table>

<h3 id="5-5-实例4：转义标识"><a href="#5-5-实例4：转义标识" class="headerlink" title="5.5 实例4：转义标识 \"></a><font size=3>5.5 实例4：转义标识 \</font></h3><p>主要是用于替换文件中的路径时用的较多。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;s/original/target/g&#x27;</span> filename</span></span><br><span class="line">sed &#x27;s/\/bin\/bash/\/abs\/df/g&#x27; ./_config.yml</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件(这里增加了第12行用于测试)</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
            [12]/bin/bash
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
            [12]/abs/df
        </td>
    </tr>
</table>

<h3 id="5-5-实例5：数据搜索并显示并执行命令"><a href="#5-5-实例5：数据搜索并显示并执行命令" class="headerlink" title="5.5 实例5：数据搜索并显示并执行命令"></a><font size=3>5.5 实例5：数据搜索并显示并执行命令</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed -n <span class="string">&#x27;/string/&#123;s/original/target/;p;q&#125;&#x27;</span> filename</span></span><br><span class="line">sed -n &#x27;/theme/&#123;s/landscape/next/;p;q&#125;&#x27; ./_config.yml</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [7] theme: theme: next theme <br>
        </td>
    </tr>
</table>


<p>【说明】该命令会找到 string 出现的第一行，再执行后面花括号中的一组命令，每个命令之间用<strong>分号分隔</strong>，这里把 landscape 替换为 next  ，p 表示显示处理结果， q 表示退出。</p>
<h2 id="6-y动作"><a href="#6-y动作" class="headerlink" title="6. y动作 "></a><font size=3>6. y动作 </font></h2><h3 id="6-1-使用格式"><a href="#6-1-使用格式" class="headerlink" title="6.1 使用格式"></a><font size=3>6.1 使用格式</font></h3><p> y  转换命令是唯一可以处理单个字符的  sed  脚本命令。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[address]y/inchars/outchars/</span><br></pre></td></tr></table></figure>

<p>转换命令会对  inchars  和  outchars  值进行一对一的映射，即  inchars  中的第一个字符会被转换为  outchars  中的第一个字符，第二个字符会被转换成  outchars  中的第二个字符…，这个映射过程会一直持续到处理完指定字符。如果  inchars  和  outchars  的长度不同，则  sed  会产生一条错误消息。</p>
<h3 id="6-2-使用实例"><a href="#6-2-使用实例" class="headerlink" title="6.2 使用实例"></a><font size=3>6.2 使用实例</font></h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line">sed &#x27;1,$y/theme:/THEME*/&#x27; datafile</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">将 1 到尾行内所有行，所有的 <span class="string">&#x27;t&#x27;</span> <span class="string">&#x27;h&#x27;</span> <span class="string">&#x27;e&#x27;</span> <span class="string">&#x27;m&#x27;</span> <span class="string">&#x27;e&#x27;</span> 5个字母分别转换成  <span class="string">&#x27;T&#x27;</span> <span class="string">&#x27;H&#x27;</span> <span class="string">&#x27;E&#x27;</span> <span class="string">&#x27;M&#x27;</span> <span class="string">&#x27;E&#x27;</span> 5个字母，将 <span class="string">&#x27;:&#x27;</span> 转换成 <span class="string">&#x27;*&#x27;</span></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">正则表达式元字符对y命令不起作用。与s命令的分隔符一样，斜线可以被替换成其它的字符。</span></span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] TiTlE* HExo <br>
            [2] auTHor* JoHn DoE <br>
            [3] languagE* En <br>
            [4] url* HTTp*//ExaMplE.coM <br>
            [5] pErMalink* *yEar/*MonTH/*day/*TiTlE/ <br>
            [6] <br>
            [7] THEME* THEME* landscapE THEME <br>
            [8] THEME* aaa <br>
            [9] THEME* landscapE <br>
            [10]THEME* landscapE <br>
            [11]THEME* dEf <br>
        </td>
    </tr>
</table>


<h2 id="7-r-动作"><a href="#7-r-动作" class="headerlink" title="7. r 动作"></a><font size=3>7. r 动作</font></h2><p>创建一个 test.txt 的文件，里边写入数据 theme: next 。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line">sed &#x27;3r test.txt&#x27; ./_config.yml # 将test.txt中的文件插入到_config.yml文件第三行后面一行</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            theme: next<br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
    </tr>
</table>


<h2 id="8-e-选项"><a href="#8-e-选项" class="headerlink" title="8. -e 选项 "></a><font size=3>8. -e 选项 </font></h2><ul>
<li>多条命令连接</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line">sed -e &#x27;2,3d&#x27; -e &#x27;s/theme/string/&#x27; ./_config.yml</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] string: theme: landscape theme <br>
            [8] string: aaa <br>
            [9] string: landscape <br>
            [10]string: landscape <br>
            [11]Theme: def <br>
        </td>
    </tr>
</table>


<p>【说明】删除第 2,3 行，然后将文件中的 theme 用 string 替换。</p>
<h2 id="9-i-选项-慎用"><a href="#9-i-选项-慎用" class="headerlink" title="9. -i 选项(慎用) "></a><font size=3>9. -i 选项(慎用) </font></h2><ul>
<li>直接修改文件</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件被修改</span></span><br><span class="line">sed -i &#x27;s/landscape/next/&#x27; ./_config.yml</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件</td>
        <td align="center">修改后的文件(屏幕无显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: next theme <br>
            [8] theme: aaa <br>
            [9] theme: next <br>
            [10]theme: next <br>
            [11]Theme: def <br>
        </td>
    </tr>
</table>


<p>【说明】可以看到，命令直接替换了所有的字符串。</p>
<h2 id="10-f-选项"><a href="#10-f-选项" class="headerlink" title="10. -f 选项 "></a><font size=3>10. -f 选项 </font></h2><ul>
<li>sed 脚本</li>
</ul>
<p> sed 脚本就是写在文件中的一列 sed 命令。脚本中，<strong>命令的末尾不能有任何多余的空格或文本</strong>。如果在一行中有多个命令，要用<strong>分号分隔</strong>。使用 sed 脚本时，<strong>不再用引号来确保 sed 命令不被 shell 解释</strong>。</p>
<p>执行脚本时， sed 先将输入文件中第一行复制到模式缓冲区，然后对其执行脚本中所有的命令。每一行处理完毕后， sed 再复制文件中下一行到模式缓冲区，对其执行脚本中所有命令。</p>
<h1 id="四、使用变量？"><a href="#四、使用变量？" class="headerlink" title="四、使用变量？"></a><font size=3>四、使用变量？</font></h1><p>我们可以在sed命令中使用变量吗？这样我们的脚本的通用性不就很高了吗。我们这里用实例来说明</p>
<h2 id="1-命令用双引号"><a href="#1-命令用双引号" class="headerlink" title="1. 命令用双引号"></a><font size=3>1. 命令用双引号</font></h2><p>第一种方式就是sed后边的命令用双引号括起来，我们可以直接引用变量，需要注意的是在变量中若有路径也要注意转义一下，例如：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;s/original/target/g&#x27;</span> filename</span></span><br><span class="line">source_path=&quot;\/bin\/bash&quot;</span><br><span class="line">target_path=&quot;ab\/cd\/ef\/gh&quot;</span><br><span class="line">sed &quot;s/$&#123;source_path&#125;/$&#123;target_path&#125;/g&quot; ./_config.yml</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件(这里增加了第12行用于测试)</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
            [12]/bin/bash
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
            [12]ab/cd/ef/gh
        </td>
    </tr>
</table>

<h2 id="2-命令用单引号"><a href="#2-命令用单引号" class="headerlink" title="2. 命令用单引号"></a><font size=3>2. 命令用单引号</font></h2><p>第二种就是命令若是单引号的话，引用的变量也要单引号括起来，需要注意的是在变量中若有路径也要注意转义一下，例如：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/sh</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意：原文件不变</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;s/original/target/g&#x27;</span> filename</span></span><br><span class="line">source_path=&quot;\/bin\/bash&quot;</span><br><span class="line">target_path=&quot;ab\/cd\/ef\/gh&quot;</span><br><span class="line">sed &#x27;s/&#x27;$&#123;source_path&#125;&#x27;/&#x27;$&#123;target_path&#125;&#x27;/g&#x27; ./_config.yml</span><br></pre></td></tr></table></figure>

<p>运行结果如下：</p>
<table>
    <tr>
        <td align="center">原文件(这里增加了第12行用于测试)</td>
        <td align="center">缓冲区输出(屏幕显示)</td>
    </tr>
    <tr>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
            [12]/bin/bash
        </td>
        <td align="left" style="vertical-align: text-top;">
            [1] title: Hexo <br>
            [2] author: John Doe <br>
            [3] language: en <br>
            [4] url: http://example.com <br>
            [5] permalink: :year/:month/:day/:title/ <br>
            [6] <br>
            [7] theme: theme: landscape theme <br>
            [8] theme: aaa <br>
            [9] theme: landscape <br>
            [10]theme: landscape <br>
            [11]Theme: def <br>
            [12]ab/cd/ef/gh
        </td>
    </tr>
</table>

<h1 id="五、空文件不生效？"><a href="#五、空文件不生效？" class="headerlink" title="五、空文件不生效？"></a><font size=3>五、空文件不生效？</font></h1><p>后来在使用过程中，遇到过这样一种情况，我需要新创建一个文件，然后直接用sed命令添加数据，我们来看一下直接这样来做会发生什么，我们由于要修改文件，所以就直接使用 -i 命令，我们这里使用向文件末尾追加的命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;\$a\string&#x27;</span> file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">注意这里使用sh会有报错，所以这里使用了bash</span></span><br><span class="line">newfile=test.txt</span><br><span class="line">data=( &quot;插入的第1行&quot;</span><br><span class="line">        &quot;插入的第2行&quot;</span><br><span class="line">        &quot;插入的第3行&quot;</span><br><span class="line">        &quot;插入的第4行&quot;</span><br><span class="line">        &quot;插入的第5行&quot;)</span><br><span class="line">touch $&#123;newfile&#125;</span><br><span class="line">echo &quot;sed 命令前的 $&#123;newfile&#125;文件内容：&quot;</span><br><span class="line">cat $&#123;newfile&#125;</span><br><span class="line">echo &quot;sed===========&gt; start&quot;</span><br><span class="line">for data_temp in &quot;$&#123;data[@]&#125;&quot;</span><br><span class="line">    do</span><br><span class="line">        echo $&#123;data_temp&#125;</span><br><span class="line">        sed -i &quot;\$a$&#123;data_temp&#125;&quot; $&#123;newfile&#125;</span><br><span class="line">    done</span><br><span class="line">echo &quot;sed===========&gt; end&quot;</span><br><span class="line">echo &quot;sed 命令后的 $&#123;newfile&#125;文件内容：&quot;</span><br><span class="line">cat $&#123;newfile&#125;</span><br></pre></td></tr></table></figure>

<p>我们可以看到终端有以下输出：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">hk@ubuntu-22-04:~/1sharedfiles/6temp$ ./1.sh </span><br><span class="line">sed 命令前的 test.txt文件内容：</span><br><span class="line">sed===========&gt; start</span><br><span class="line">插入的第1行</span><br><span class="line">插入的第2行</span><br><span class="line">插入的第3行</span><br><span class="line">插入的第4行</span><br><span class="line">插入的第5行</span><br><span class="line">sed===========&gt; end</span><br><span class="line">sed 命令后的 test.txt文件内容：</span><br></pre></td></tr></table></figure>

<p>我们会发现文件依然是空的，没有任何数据，网上查了一下，发现是因为sed是基于行来处理的文件流编辑器，如果文件为空的话，它是处理不了的，那我们怎么处理呢？我们可以判断一下，当是空文件的时候使用echo来写，非空的时候用sed追加：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">sed [ option ] [ action ] file_name</span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">格式：sed <span class="string">&#x27;$a\string&#x27;</span> file_name</span></span><br><span class="line">newfile=test.txt</span><br><span class="line">data=( &quot;插入的第1行&quot;</span><br><span class="line">        &quot;插入的第2行&quot;</span><br><span class="line">        &quot;插入的第3行&quot;</span><br><span class="line">        &quot;插入的第4行&quot;</span><br><span class="line">        &quot;插入的第5行&quot;)</span><br><span class="line">touch $&#123;newfile&#125;</span><br><span class="line">echo &quot;sed 命令前的 $&#123;newfile&#125;文件内容：&quot;</span><br><span class="line">cat $&#123;newfile&#125;</span><br><span class="line">echo &quot;sed===========&gt; start&quot;</span><br><span class="line">for data_temp in &quot;$&#123;data[@]&#125;&quot;</span><br><span class="line">    do</span><br><span class="line">        echo $&#123;data_temp&#125;</span><br><span class="line">        # test -s $&#123;newfile&#125; &amp;&amp; sed -i &quot;\$a$&#123;data_temp&#125;&quot; $&#123;newfile&#125; || echo &quot;$&#123;data_temp&#125;&quot; &gt;&gt; $&#123;newfile&#125;</span><br><span class="line">        if test -s $&#123;newfile&#125;;then</span><br><span class="line">            sed -i &quot;\$a$&#123;data_temp&#125;&quot; $&#123;newfile&#125;</span><br><span class="line">        else</span><br><span class="line">            echo &quot;$&#123;data_temp&#125;&quot; &gt;&gt; $&#123;newfile&#125;</span><br><span class="line">        fi</span><br><span class="line">    done</span><br><span class="line">echo &quot;sed===========&gt; end&quot;</span><br><span class="line">echo &quot;sed 命令后的 $&#123;newfile&#125;文件内容：&quot;</span><br><span class="line">cat $&#123;newfile&#125;</span><br></pre></td></tr></table></figure>

<p>执行的结果如下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">hk@ubuntu-22-04:~/1sharedfiles/6temp$ ./1.sh </span><br><span class="line">sed 命令前的 test.txt文件内容：</span><br><span class="line">sed===========&gt; start</span><br><span class="line">插入的第1行</span><br><span class="line">插入的第2行</span><br><span class="line">插入的第3行</span><br><span class="line">插入的第4行</span><br><span class="line">插入的第5行</span><br><span class="line">sed===========&gt; end</span><br><span class="line">sed 命令后的 test.txt文件内容：</span><br><span class="line">插入的第1行</span><br><span class="line">插入的第2行</span><br><span class="line">插入的第3行</span><br><span class="line">插入的第4行</span><br><span class="line">插入的第5行</span><br></pre></td></tr></table></figure>





<p>【说明】文章到这里就暂时结束了，没有遇到过相应的使用方式，笔记先到这里， sed 还有其他的用法，后续有用到再进行补充。</p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/9720d703.html">LV02-02-shell-07-sed命令的使用</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2022年02月22日 - 08:22</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/9720d703.html" title="LV02-02-shell-07-sed命令的使用">https://sumumm.github.io/post/9720d703.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV02-%E7%BC%96%E8%AF%91%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> LV02-编译基础</a>
              <a href="/tags/shell%E8%84%9A%E6%9C%AC/" rel="tag"><i class="fa fa-tag"></i> shell脚本</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/a638936c.html" rel="prev" title="LV02-04-GDB-调试工具">
                  <i class="fa fa-angle-left"></i> LV02-04-GDB-调试工具
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/ab474d7d.html" rel="next" title="LV02-02-shell-06-各种括号总结">
                  LV02-02-shell-06-各种括号总结 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
